package hadoop;
import java.io.IOException;
import java.net.UnknownHostException;

import lucene_ik.Index;
import lucene_ik.Item;
import lucene_ik.json;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.ipc.RPC.Server;
import org.json.JSONException;

public class MyServer implements MyProtocol{
    private Server server;
    
    public MyServer(){
        try {
            server = RPC.getServer(this, "localhost", 8080, new Configuration());
            server.start();
            server.join();
            
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public Text println(Text t){
        System.out.println(t);
        return new Text("finish");
    }
    public Text IndexItem(String s){
    	Item item;
		try {
			item = json.fromjson(s);
			Index index=new Index(item);
			System.out.print("success");
			return new Text("finish");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Text("error");
		}
		
		
    }

    @Override
    public long getProtocolVersion(String protocol, long clientVersion) throws IOException {
        return 1;
    }
    
    public static void main(String[] args) {
    	new MyServer();
    	
    	
        
    }

}